import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutterprovider/bean/user_bean.dart';

class ChangeUserInfoPage extends StatefulWidget {
  @override
  _ChangeUserInfoPageState createState() => _ChangeUserInfoPageState();
}

class _ChangeUserInfoPageState extends State<ChangeUserInfoPage> {
  String _name;
  String _address;
  int _age;
  String textStr = "修改用户信息 provider监听";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("第二个页面"),
      ),
      body: Center(
        child: Container(
          padding: EdgeInsets.only(left: 20, right: 20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(
                    gapPadding: 10.0,
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  labelText: "请修改用户名",
                  helperText: Provider.of<Userbean>(context).name,
                  prefixIcon: Icon(Icons.people),
                ),
                onChanged: (v) {
                  print("onchange==>" + v);
                  setState(() {
                    this._name = v;
                  });
                },
                onSubmitted: (v) {
                  print("onSubmitted==>" + v);
                },
              ),
              SizedBox(
                height: 10,
              ),
              TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(
                    gapPadding: 10.0,
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  labelText: "请修改地址",
                  helperText: Provider.of<Userbean>(context).address,
                  prefixIcon: Icon(Icons.people),
                ),
                onChanged: (v) {
                  print("onchange==>" + v);
                  setState(() {
                    this._address = v;
                  });
                },
                onSubmitted: (v) {
                  print("onSubmitted==>" + v);
                },
              ),
              SizedBox(
                height: 10,
              ),
              TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(
                    gapPadding: 10.0,
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  labelText: "请修改年龄",
                  helperText: "${Provider.of<Userbean>(context).age}",
                  prefixIcon: Icon(Icons.people),
                ),
                onChanged: (v) {
                  print("onchange==>" + v);
                  setState(() {
                    this._age = int.parse(v);
                  });
                },
                onSubmitted: (v) {
                  print("onSubmitted==>" + v);
                },
              ),
              FlatButton(
                color: Colors.lightBlueAccent,
                textColor: Colors.white,
                child: Text(textStr),
                onPressed: () {
                  String name = "";
                  if (_name == null || _name.length <= 0) {
                    name = Provider.of<Userbean>(context, listen: false).name;
                  } else {
                    name = _name;
                  }
                  String address = "";
                  if (_address == null || _address.length <= 0) {
                    address =
                        Provider.of<Userbean>(context, listen: false).address;
                  } else {
                    address = _address;
                  }

                  Provider.of<Userbean>(context, listen: false)
                      .update(name, address, _age);
                  setState(() {
                    textStr = "2秒后自动关闭页面";
                  });
                  Future.delayed(Duration(seconds: 2), () {
                    Navigator.pop(context);
                  });
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
